<template>
  <div class="main">
    <PureTableBar title="固件列表" :columns="columns" @refresh="onSearch">
      <!-- 表格操作栏 -->
      <template #buttons>
        <el-button
          type="primary"
          @click="uploadFirmwareButton(firmwareAddForm, uploadForm)"
        >
          上传固件
        </el-button>
      </template>
      <template v-slot="{ size, dynamicColumns }">
        <pure-table
          border
          :size="size"
          :columns="dynamicColumns"
          align-whole="center"
          showOverflowTooltip
          table-layout="auto"
          :loading="loading"
          adaptive
          :data="dataList"
          :pagination="pagination"
          @page-size-change="getFirmwareList"
          @page-current-change="getFirmwareList"
          :header-cell-style="{
            background: 'var(--el-table-row-hover-bg-color)',
            color: 'var(--el-text-color-primary)'
          }"
        >
          <template #operation="{ row }">
            <el-popconfirm
              :hide-after="10"
              :title="`是否确认删除ID为${row.id}的固件`"
              @confirm="deleteFirmwareFunction(row.id)"
            >
              <template #reference>
                <el-button
                  class="reset-margin"
                  link
                  type="danger"
                  :size="size"
                >
                  删除
                </el-button>
              </template>
            </el-popconfirm>
          </template>
        </pure-table>
      </template>
    </PureTableBar>
    <el-dialog v-model="dialogVisible" title="固件添加" width="40%" draggable>
      <el-form
        ref="firmwareAddForm"
        :inline="false"
        label-width="100px"
        class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px]"
        :model="FileUploadParams"
      >
        <el-form-item label="固件版本：" prop="version" label-width="150px">
          <el-input
            v-model="FileUploadParams.version"
            placeholder="请输入固件版本"
            style="width: 20vw"
          />
        </el-form-item>
        <el-form-item label="上传者：" prop="uploadUser" label-width="150px">
          <el-input
            v-model="FileUploadParams.uploadUser"
            placeholder="请输入上传者"
            style="width: 20vw"
          />
        </el-form-item>

        <el-form-item label="描述：" prop="remark" label-width="150px">
          <el-input
            v-model="FileUploadParams.remark"
            :rows="2"
            style="width: 20vw"
            type="textarea"
            placeholder="请输入固件描述"
          />
        </el-form-item>
        <el-form-item label="固件文件：" label-width="150px">
          <el-upload
            ref="uploadForm"
            :limit="1"
            :auto-upload="false"
            action="#"
            @change="
              e => {
                fileData.data = e;
              }
            "
          >
            <template #trigger>
              <el-button style="width: 20vw" type="primary">选择固件</el-button>
            </template>
          </el-upload>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="uploadFileFunction">
            提交
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { PureTableBar } from "@/components/RePureTableBar";
import { defineOptions, ref } from "vue";
import {useFirmwareHooks} from "@/views/firmware/utils/firmwareHooks";
defineOptions({
  name: "firmwareList"
});
const firmwareAddForm = ref();
const uploadForm = ref();
const {
  loading,
  columns,
  dataList,
  pagination,
  onSearch,
  dialogVisible,
  getFirmwareList,
  FileUploadParams,
  uploadFileFunction,
  fileData,
  uploadFirmwareButton,
  deleteFirmwareFunction,
  updataButtonRemark,
  downloadFirmware,
} = useFirmwareHooks();
</script>

<style scoped lang="scss">
:deep(.el-dropdown-menu__item i) {
  margin: 0;
}

.search-form {
  :deep(.el-form-item) {
    margin-bottom: 12px;
  }
}
</style>
